const Trie = require("../template")

function testTriePrefixMatching() {
  const trie = new Trie()

  console.log(
    "1. 单字符前缀匹配测试:",
    (() => {
      trie.insert("apple")
      trie.insert("application")
      trie.insert("apply")
      trie.insert("banana")
      const words = trie.getWordsWithPrefix("a")
      const expected = ["apple", "application", "apply"]
      return JSON.stringify(words.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "2. 多字符前缀匹配测试:",
    (() => {
      const prefixTrie = new Trie()
      prefixTrie.insert("cat")
      prefixTrie.insert("car")
      prefixTrie.insert("card")
      prefixTrie.insert("care")
      prefixTrie.insert("careful")
      const words = prefixTrie.getWordsWithPrefix("car")
      const expected = ["car", "card", "care", "careful"]
      return JSON.stringify(words.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "3. 不存在前缀测试:",
    (() => {
      const nonExistentTrie = new Trie()
      nonExistentTrie.insert("hello")
      nonExistentTrie.insert("world")
      const words = nonExistentTrie.getWordsWithPrefix("xyz")
      return words.length === 0
    })()
  )

  console.log(
    "4. 空前缀测试:",
    (() => {
      const emptyPrefixTrie = new Trie()
      emptyPrefixTrie.insert("a")
      emptyPrefixTrie.insert("ab")
      emptyPrefixTrie.insert("abc")
      const words = emptyPrefixTrie.getWordsWithPrefix("")
      const expected = ["a", "ab", "abc"]
      return JSON.stringify(words.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "5. 完整单词前缀测试:",
    (() => {
      const completeTrie = new Trie()
      completeTrie.insert("hello")
      completeTrie.insert("hell")
      completeTrie.insert("help")
      const words = completeTrie.getWordsWithPrefix("hello")
      const expected = ["hello"]
      return JSON.stringify(words.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "6. 前缀匹配与搜索一致性测试:",
    (() => {
      const consistencyTrie = new Trie()
      consistencyTrie.insert("test")
      consistencyTrie.insert("testing")
      consistencyTrie.insert("tester")
      const hasPrefix = consistencyTrie.startsWith("test")
      const words = consistencyTrie.getWordsWithPrefix("test")
      return hasPrefix === true && words.length === 3
    })()
  )

  console.log(
    "7. this上下文前缀匹配测试:",
    (() => {
      const prefixObj = {
        multiplier: 2,
        processPrefix: function(words, prefix) {
          const trie = new Trie()
          words.forEach(word => trie.insert(word))
          const prefixWords = trie.getWordsWithPrefix(prefix)
          return prefixWords.length * this.multiplier
        }
      }
      return prefixObj.processPrefix(["cat", "car", "card"], "car") === 4 // 2 * 2 = 4
    })()
  )

  console.log(
    "8. 复杂对象this绑定前缀匹配测试:",
    (() => {
      const prefixConfigObj = {
        config: { minLength: 3 },
        processWithConfig: function(words, prefix) {
          const trie = new Trie()
          words.forEach(word => trie.insert(word))
          const prefixWords = trie.getWordsWithPrefix(prefix)
          return prefixWords.filter(word => word.length >= this.config.minLength).length
        }
      }
      return prefixConfigObj.processWithConfig(["a", "ab", "abc", "abcd"], "a") === 2
    })()
  )

  console.log(
    "9. 前缀匹配性能测试:",
    (() => {
      const performanceTrie = new Trie()
      const words = Array.from({ length: 1000 }, (_, i) => `word${i}`)
      words.forEach(word => performanceTrie.insert(word))
      
      const start = performance.now()
      for (let i = 0; i < 100; i++) {
        performanceTrie.getWordsWithPrefix("word")
      }
      const end = performance.now()
      
      return (end - start) < 1000
    })()
  )

  console.log(
    "10. 前缀匹配边界测试:",
    (() => {
      const boundaryTrie = new Trie()
      boundaryTrie.insert("a")
      boundaryTrie.insert("aa")
      boundaryTrie.insert("aaa")
      const words1 = boundaryTrie.getWordsWithPrefix("a")
      const words2 = boundaryTrie.getWordsWithPrefix("aa")
      const words3 = boundaryTrie.getWordsWithPrefix("aaa")
      const words4 = boundaryTrie.getWordsWithPrefix("aaaa")
      
      return words1.length === 3 &&
             words2.length === 2 &&
             words3.length === 1 &&
             words4.length === 0
    })()
  )

  console.log(
    "11. 前缀匹配复杂测试:",
    (() => {
      const complexTrie = new Trie()
      complexTrie.insert("programming")
      complexTrie.insert("program")
      complexTrie.insert("programmer")
      complexTrie.insert("progress")
      complexTrie.insert("project")
      const words = complexTrie.getWordsWithPrefix("prog")
      const expected = ["programming", "program", "programmer", "progress"]
      return JSON.stringify(words.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "12. 前缀匹配删除后测试:",
    (() => {
      const deleteTrie = new Trie()
      deleteTrie.insert("hello")
      deleteTrie.insert("help")
      deleteTrie.insert("hell")
      deleteTrie.delete("help")
      const words = deleteTrie.getWordsWithPrefix("hel")
      const expected = ["hello", "hell"]
      return JSON.stringify(words.sort()) === JSON.stringify(expected.sort())
    })()
  )
}

testTriePrefixMatching()
